package com.manage.base.listener;

import java.io.IOException;
import java.util.Timer;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.manage.base.mongo.MongoManager;
import com.manage.tasks.ActCheckTask;
import com.manage.tasks.ActivityPushTask;
import com.manage.tasks.NewsCheckTask;
import com.manage.util.SystemUtil;

public class RedisLoaderListener implements ServletContextListener {

	protected static final Logger LOG = LoggerFactory.getLogger(RedisLoaderListener.class);

	public static ApplicationContext ctx;

	/*
	 * @see
	 * javax.servlet.ServletContextListener#contextInitialized(javax.servlet
	 * .ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent event) {
		LOG.info("------------------Redis data init start------------------");
		ServletContext context = event.getServletContext();
		ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		setupContext(context);
		LOG.info("------------------Redis data init end--------------------");
		if (SystemUtil.isMongoEnable()) {
			LOG.info("------------------Mongo init start------------------");
			try {
				MongoManager.init();
			} catch (IOException e) {
				throw new RuntimeException(e.getMessage(), e);
			}
			LOG.info("------------------Mongo init end--------------------");
		}
		startTask();
		LOG.info("------------------Task init end--------------------");
	}

	private void startTask() {
		Timer t = new Timer("tasktimer");
		ActCheckTask t1 = new ActCheckTask();
		t1.setName("actCheckTask");
		NewsCheckTask t2 = new NewsCheckTask();
		t2.setName("newsCheckTask");
		ActivityPushTask t3 = new ActivityPushTask();
		String actCheckPeriod = SystemUtil.getProp("task_ActCheckTask_time");
		String newsCheckPeriod = SystemUtil.getProp("task_NewsCheckTask_time");
		String actPushPeriod = SystemUtil.getProp("task_NewsCheckTask_time");
		if (actCheckPeriod != null && !actCheckPeriod.equals("") && !actCheckPeriod.equals("-1")) {
			t.schedule(t1, 5000, Long.parseLong(actCheckPeriod) * 1000);
		} else {
			LOG.info("Param task_ActCheckTask_time is null ,please check app.properties");
		}
		if (newsCheckPeriod != null && !newsCheckPeriod.equals("") && !newsCheckPeriod.equals("-1")) {
			t.schedule(t2, 5000, Long.parseLong(newsCheckPeriod) * 1000);
		} else {
			LOG.info("Param task_NewsCheckTask_time is null ,please check app.properties");
		}
		if (actPushPeriod != null && !actPushPeriod.equals("") && !actPushPeriod.equals("-1")) {
			t.schedule(t3, 5000, Long.parseLong(actPushPeriod) * 1000);
		} else {
			LOG.info("Param task_ActPushTask_time is null ,please check app.properties");
		}
	}

	/*
	 * 初始化权限相关的东西(包括所有角色list,所有url对应角色map)
	 * 
	 * @param context The servlet context
	 */
	public static void setupContext(ServletContext context) {
		// final RedisTemplate<Serializable, Serializable> redisTemplate =
		// (RedisTemplate) ctx
		// .getBean("redisTemplate");
		//
		// IUserDao userDao = (IUserDao) ctx.getBean("userDao");
		// List list = userDao.select(new User());
		// redisTemplate.execute(new RedisCallback<Object>() {
		//
		// @Override
		// public Object doInRedis(RedisConnection connection)
		// throws DataAccessException {
		// connection
		// .set(
		// redisTemplate.getStringSerializer().serialize(
		// "user.uid.1"), redisTemplate.getStringSerializer()
		// .serialize("1111"));
		// return null;
		// }
		// });
	}

	public void contextDestroyed(ServletContextEvent servletContextEvent) {
	}

}
